'use strict';

class Detail {
    constructor(clusters,detail, serviceSrv , alarmSrv, ctgNotification, $window, $timeout,$stateParams,$state) {
        'ngInject';
        this._notification    = ctgNotification;
        this.serviceSrv       = serviceSrv;
        this.alarmSrv         = alarmSrv;
        this.$stateParams     = $stateParams;
        this._state           = $state;
        this.selectList       = [];
        this.nonSelectObError = true;
        this.ruleList         = [];
        this.detail           = detail;
        this.optsTypes        = [{type:'lt',name:'<'},{type:'gt',name:'>'},{type:'lte',name:'<='},{type:'gte',name:'>='},{type:'rt',name:'范围'},{type:'rte',name:'范围等于'}]
        this.clusters         = clusters && clusters.list;
        this.clusterInst      = clusters && clusters.list.length && clusters.list[0];
        this.data             = {};
        this.selectNames      = [];
        this.loading          = false;
        this.page = {
            page: 1
        };


        $timeout(function () {
            angular.element('.table-fixed-box').css({
                'max-height': ($window.innerHeight - 450) + 'px'
            });
        }, 0, false);


        if(detail){
            this.edit = true;
            this.data = detail;
            this.selectList = detail.service;
            if(this.selectList && this.selectList.length!=0){
                this.showRc=true;
            }else{
                this.showRc=false;
            }
        }else{
            this.edit = false;
        }

        this.getAlertTypes();
        this.getRCList();

    }

    getAlertTypes() {
        this.ruleLoading = true;
        this.alarmSrv.getAlertTypes({clusterCode : this.clusterInst.clusterCode}).then(data=> {
            this.ruleType = data;
            this.ruleLoading = false;
            if(this.edit){
                angular.forEach(this.detail.ruleItem, (ele)=>{
                    let ruleIndex;
                    angular.forEach(this.ruleType, (elem,index)=>{
                        if(elem.type == ele.type ){
                            ruleIndex = index;
                        }
                    })
                    angular.extend(ele, {
                        typeList:this.ruleType,
                        optsList:this.optsTypes,
                        typeItem:this.ruleType[ruleIndex]
                    })
                    this.ruleList.push(ele)
                })
            }else{
                this.ruleList.push(this.ruleTemplet());
            }
        }).catch(()=> {
            this._notification.error('获取告警类型列表数据有误.');
        });
    }

    ruleTemplet(){
        return {
            typeList:this.ruleType,
            optsList:this.optsTypes,
            typeItem:this.ruleType[0],
            type:this.ruleType[0].type,
            opts:this.optsTypes[0].type,
            alertItemId:this.ruleType[0].id,
            typeDescription:this.ruleType[0].description,
            unit : this.ruleType[0].unit
        }
    }

    getRCList(){
        this.serviceSrv.getRCList({
            perPage: 999,
            page: this.page.page,
            code: this.clusterInst.clusterCode
        }).then(data=> {
            this.rcList = data.replicationControllers;
            if(this.edit){
                angular.forEach(this.selectList, (ele)=>{
                    this.selectNames.push(ele.serviceName);
                })
                angular.forEach(this.rcList, (ele,index)=>{
                    if(this.selectNames.indexOf(ele.name)>-1){
                        ele.checked = true;
                    }
                })
            }
        }).catch(data=> {
            this._notification.error('获取服务列表数据有误.');
        });


    }

    cancel() {
        this._modalInstance.dismiss('cancel');
    }

    selectObItem(rcItem) {
        if (rcItem.checked) {
            this.selectList.splice(this.selectList.indexOf(rcItem), 1);
            rcItem.checked = false;
            if (!this.selectList.length) this.nonSelectObError = true;
        } else {
            rcItem.checked = true;
            this.selectList.push({serviceName:rcItem.name,serviceNamespace:rcItem.namespace});
            if (this.nonSelectObError) this.nonSelectObError = false;
        }
    }

    unObSel(index,serviceName){
        angular.forEach(this.rcList, (ele,index)=>{
            if(ele.name == serviceName){
                ele.checked = false;
            }
        })
        // this.rcList[this.rcList.indexOf(this.selectList[index])].checked = false;

        this.selectList.splice(index, 1);
        if (!this.selectList.length) {
            this.nonSelectObError = true;
            this.selListExpend = false;
        }
    }
    expendSelList(){
        this.selListExpend = !this.selListExpend;
    }

    changeType(elem){
        // this.selectList = [];
    }

    save(){
        this.loading = true;

        angular.forEach(this.ruleList, (ele,index)=>{
            delete ele.typeList;
            delete ele.optsList;
            delete ele.typeItem;
        })

        this.filter = {
            clusterCode : this.clusterInst.clusterCode,
            alertRuleDTO: {
                alertName   : this.data.alertName,
                phone       : this.data.phone,
                email       : this.data.email,
                id          : this.edit?this.$stateParams.id:'',
                ruleItem    : this.ruleList,
                service     : this.showRc?this.selectList:[]
            }
        }


        if(this.edit){
            this.alarmSrv.editAlertRule(this.filter).then(data=> {
                this.loading = false;
                this._state.go('alarm-k8s-pre');
            }).catch(()=> {
                this._notification.error('修改告警规则有误.');
            });
        }else{
            this.alarmSrv.addAlertRule(this.filter).then(data=> {
                this.loading = false;
                this._state.go('alarm-k8s-pre');
            }).catch(()=> {
                this._notification.error('新增告警规则有误.');
            });
        }

    }



    addRule(){
        this.ruleList.push(this.ruleTemplet())
    }

    delRule($index){
        if(this.ruleList.length>1){
            this.ruleList.splice($index,1);
        }

    }

    ruleCheck(elem){
        if(elem.opts=='rt' || elem.opts=='rte'){
            if(!elem.min || !elem.max){
                this.ruleDanger = '请输入正确的数值';
                return true;
            }
            if(elem.max<=elem.min){
                this.ruleDanger = '最大值不能小于最小值';
                return true;
            }

        }else{
            if(!elem.min){
                this.ruleDanger = '请输入正确的数值';
                return true;
            }
        }

        if(elem.max>100 || elem.max>100){
            this.ruleDanger = '请输入100以内数值';
            return true;
        }



    }

}


export default Detail